(ns controllers.baza
  (:require [clojure.java.jdbc :as sql]
            ))

 
(let [db-host "localhost"
      db-port 3306
      db-name "users"]
  
  (def db {:classname "com.mysql.jdbc.Driver" 
           :subprotocol "mysql"
           :subname (str "//" db-host ":" db-port  "/" db-name)       
           :user "root"
           :password ""}))

(defn create-users-table-command
  []
  (sql/create-table
   :users
   [:id :integer "PRIMARY KEY" "AUTO_INCREMENT"]
   [:username :text];"varchar(255)"
   [:pass :text]
   [:mail :text]))

 (defn create-table-users
   []
	   (sql/with-connection
		   db
		   (sql/transaction
		     (create-users-table-command))))

 (defn find-uniqe[index]
   
   (sql/with-connection db
	  (sql/with-query-results rows
	    ["SELECT * FROM users WHERE id = ?" index]
	    (:mail (first rows))))
   
   )
 
 (defn find-kor
   [user]   
   (sql/with-connection db
	  (sql/with-query-results rows
	    ["SELECT * FROM users WHERE username = ?" user]
	    (:pass (first rows))))
   
   )
 
 (defn insert-user ;treba da vrati neki signal
   [user pass mail]
		(sql/with-connection db
		  (sql/insert-records :users
		    {:username user :pass pass :mail mail}
      )
    )
  )
 
(defn update
   [id user pass mail]
		(sql/with-connection db
		  (sql/update-values :users
                       ["id=?" id]
		    {:username user :pass pass :mail mail}
      )
    )
  )

(defn delete
  
  [id]
  (sql/with-connection db
    (sql/delete-rows :users ["id=?" id])))
 



;(use '[clojure.java.io :only [input-stream]])

;(defn read-file[name]
;  (def g (input-stream name))
;  (.read g)
;  )

;pisanje falojva u bazu

(comment 
  ;http://en.wikibooks.org/wiki/Clojure_Programming/Examples/JDBC_Examples#MySQL_2
  ;sta treba odraditi:
  
  ;;;;+;obrada slike - primena filtera;operacije
  
  ;web stuff:
  ;registracija korisnika - upisivanje u fajl;upisivanje u bazu
  ;logovanje korisnika - proveravanje u fajlu/bazi  
  ;preuzimanje slike - download
  ;rad sa bazom
  
  )